//
//  App.swift
//  mac-wky
//
//  Created by Apple on 2018/2/7.
//  Copyright © 2018年 Apple. All rights reserved.
//

import Foundation
import Cocoa
class App {
    /// 获取cookie
    ///
    /// - Parameter name: cookie的名称
    /// - Returns: cookie
    static func getCookie(name: String) -> String {
        guard let cookies = HTTPCookieStorage.shared.cookies else { return "" }
        for cookie in cookies {
            if cookie.name == name { return cookie.value }
        }
        return ""
    }
    /// 清空cookie
    static func clearCookie() {
        HTTPCookieStorage.shared.cookies?.forEach { HTTPCookieStorage.shared.deleteCookie($0) }
    }
    
    /// 随机数
    ///
    /// - Parameter count: 位数
    /// - Returns: 随机数字符串
    static func random(count: Int) -> String {
        var str = ""
        for _ in 0..<count { str += String(format:"%d", arc4random_uniform(10)) }
        return str
    }
    
    /// 提示框
    ///
    /// - Parameter msg: 提示内容
    static func alert(_ msg: String) {
        let alert = NSAlert()
        alert.messageText = msg
        alert.addButton(withTitle: "确定")
        alert.alertStyle = .critical
        alert.beginSheetModal(for: NSApplication.shared.mainWindow!, completionHandler: nil)
    }
}
